<template>
  <div class="app-container" v-if="isAuthenticated">
    <el-container>
      <!-- 顶部导航栏 -->
      <el-header class="app-header">
        <div class="header-content">
          <div class="logo">
            <el-icon class="logo-icon"><Wallet /></el-icon>
            <span class="logo-text">Web3 钱包</span>
          </div>
          <div class="header-right">
            <el-badge :value="12" class="notification-badge">
              <el-icon class="notification-icon"><Bell /></el-icon>
            </el-badge>
            <el-dropdown @command="handleCommand" trigger="click">
              <span class="user-dropdown">
                <el-avatar :size="36" :src="userInfo?.avatar || ''" class="user-avatar">
                  {{ userInfo?.username?.charAt(0).toUpperCase() || 'U' }}
                </el-avatar>
                <span class="username">{{ userInfo?.username || '未登录' }}</span>
                <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">
                    <el-icon><User /></el-icon>
                    <span>个人中心</span>
                  </el-dropdown-item>
                  <el-dropdown-item command="settings">
                    <el-icon><Setting /></el-icon>
                    <span>设置</span>
                  </el-dropdown-item>
                  <el-dropdown-item divided command="logout">
                    <el-icon><SwitchButton /></el-icon>
                    <span>退出登录</span>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>

      <el-container>
        <!-- 侧边栏菜单 -->
        <el-aside :width="isCollapse ? '64px' : '220px'" class="app-aside">
          <div class="sidebar-toggle" @click="toggleSidebar">
            <el-icon><Fold v-if="!isCollapse" /><Expand v-else /></el-icon>
          </div>
          <el-menu
            :default-active="activeMenu"
            router
            :collapse="isCollapse"
            class="sidebar-menu"
            background-color="#1a1d29"
            text-color="#a0aec0"
            active-text-color="#409eff"
            :unique-opened="true"
          >
            <el-menu-item index="/dashboard">
              <el-icon><Odometer /></el-icon>
              <template #title>仪表盘</template>
            </el-menu-item>
            <el-menu-item index="/wallet">
              <el-icon><Wallet /></el-icon>
              <template #title>我的钱包</template>
            </el-menu-item>
            <el-menu-item index="/deposit">
              <el-icon><Plus /></el-icon>
              <template #title>充值</template>
            </el-menu-item>
            <el-menu-item index="/transfer">
              <el-icon><Right /></el-icon>
              <template #title>转账</template>
            </el-menu-item>
            <el-menu-item index="/withdraw">
              <el-icon><Download /></el-icon>
              <template #title>提现</template>
            </el-menu-item>
            <el-menu-item index="/transactions">
              <el-icon><List /></el-icon>
              <template #title>交易记录</template>
            </el-menu-item>
            <el-menu-item index="/identity">
              <el-icon><User /></el-icon>
              <template #title>实名认证</template>
            </el-menu-item>
            <el-sub-menu index="/c2c">
              <template #title>
                <el-icon><ShoppingBag /></el-icon>
                <span>C2C交易</span>
              </template>
              <el-menu-item index="/c2c">
                <el-icon><ShoppingBag /></el-icon>
                <template #title>订单列表</template>
              </el-menu-item>
              <el-menu-item index="/c2c/publish">
                <el-icon><Plus /></el-icon>
                <template #title>发布订单</template>
              </el-menu-item>
              <el-menu-item index="/c2c/my-orders">
                <el-icon><List /></el-icon>
                <template #title>我的订单</template>
              </el-menu-item>
              <el-menu-item index="/c2c/my-trades">
                <el-icon><ShoppingCart /></el-icon>
                <template #title>我的交易</template>
              </el-menu-item>
            </el-sub-menu>
          </el-menu>
        </el-aside>

        <!-- 主内容区 -->
        <el-main class="app-main">
          <div class="main-content">
            <router-view v-slot="{ Component }">
              <transition name="fade" mode="out-in">
                <component :is="Component" />
              </transition>
            </router-view>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
  <router-view v-else />
</template>

<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  Wallet,
  ArrowDown,
  Odometer,
  Plus,
  Right,
  List,
  Download,
  Bell,
  User,
  Setting,
  SwitchButton,
  Fold,
  Expand,
  ShoppingBag,
  ShoppingCart
} from '@element-plus/icons-vue'
import { useAuthStore } from './store/auth'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()

const activeMenu = computed(() => route.path)
const userInfo = computed(() => authStore.userInfo)
const isAuthenticated = computed(() => authStore.isAuthenticated)
const isCollapse = ref(false)

onMounted(async () => {
  // 检查登录状态
  if (!authStore.isAuthenticated) {
    router.push('/login')
  } else {
    // 获取用户信息
    await authStore.getUserProfile()
  }
})

const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}

const handleCommand = (command: string) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'settings':
      router.push('/settings')
      break
    case 'logout':
      authStore.logout()
      router.push('/login')
      ElMessage.success('已退出登录')
      break
  }
}
</script>

<style scoped>
.app-container {
  height: 100vh;
  overflow: hidden;
  background: #f5f7fa;
}

.app-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-bottom: none;
  padding: 0;
  height: 64px;
  line-height: 64px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 100;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  height: 100%;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  font-size: 24px;
  color: #fff;
}

.logo-text {
  font-size: 20px;
  font-weight: 600;
  color: #fff;
  letter-spacing: 0.5px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.notification-badge {
  cursor: pointer;
}

.notification-icon {
  font-size: 20px;
  color: #fff;
  cursor: pointer;
  transition: transform 0.2s;
}

.notification-icon:hover {
  transform: scale(1.1);
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 20px;
  transition: all 0.3s;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.user-dropdown:hover {
  background: rgba(255, 255, 255, 0.2);
}

.user-avatar {
  border: 2px solid rgba(255, 255, 255, 0.3);
}

.username {
  font-size: 14px;
  color: #fff;
  font-weight: 500;
}

.dropdown-icon {
  font-size: 12px;
  color: #fff;
  transition: transform 0.3s;
}

.app-aside {
  background-color: #1a1d29;
  height: calc(100vh - 64px);
  overflow: hidden;
  transition: width 0.3s;
  position: relative;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

.sidebar-toggle {
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #a0aec0;
  transition: all 0.3s;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.sidebar-toggle:hover {
  background-color: rgba(255, 255, 255, 0.05);
  color: #409eff;
}

.sidebar-menu {
  border: none;
  height: calc(100% - 48px);
  overflow-y: auto;
  overflow-x: hidden;
}

.sidebar-menu:not(.el-menu--collapse) {
  width: 220px;
}

.sidebar-menu .el-menu-item {
  height: 56px;
  line-height: 56px;
  margin: 4px 8px;
  border-radius: 8px;
  transition: all 0.3s;
}

.sidebar-menu .el-menu-item:hover {
  background-color: rgba(64, 158, 255, 0.1) !important;
  color: #409eff !important;
}

.sidebar-menu .el-menu-item.is-active {
  background-color: rgba(64, 158, 255, 0.2) !important;
  color: #409eff !important;
  font-weight: 500;
}

.sidebar-menu .el-icon {
  font-size: 18px;
  margin-right: 8px;
}

.app-main {
  background-color: #f5f7fa;
  padding: 0;
  height: calc(100vh - 64px);
  overflow-y: auto;
}

.main-content {
  padding: 24px;
  min-height: calc(100vh - 64px);
}

/* 滚动条样式 */
.sidebar-menu::-webkit-scrollbar,
.app-main::-webkit-scrollbar {
  width: 6px;
}

.sidebar-menu::-webkit-scrollbar-track,
.app-main::-webkit-scrollbar-track {
  background: transparent;
}

.sidebar-menu::-webkit-scrollbar-thumb,
.app-main::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 3px;
}

.sidebar-menu::-webkit-scrollbar-thumb:hover,
.app-main::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.2);
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s, transform 0.3s;
}

.fade-enter-from {
  opacity: 0;
  transform: translateY(10px);
}

.fade-leave-to {
  opacity: 0;
  transform: translateY(-10px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .app-aside {
    position: absolute;
    left: 0;
    top: 64px;
    z-index: 99;
    box-shadow: 2px 0 12px rgba(0, 0, 0, 0.15);
  }
  
  .logo-text {
    display: none;
  }
  
  .username {
    display: none;
  }
  
  .main-content {
    padding: 16px;
  }
}
</style>

